package com.night.icm.controller.base;

import com.night.icm.common.utils.PageDataDTO;
import com.night.icm.common.utils.SimpleDataDTO;
import com.night.icm.model.base.AcademyDTO;
import com.night.icm.service.base.service.AcademyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import java.util.List;

/**
 * AcademyController
 *
 * @author Liao ZhiYong
 * @date 2019/12/21
 */
@Api(tags = {"学院管理"})
@RestController
@RequestMapping("/api/icm-academy")
public class AcademyController {

    private final AcademyService academyService;

    @Autowired
    public AcademyController(AcademyService academyService) {
        this.academyService = academyService;
    }

    @ApiOperation(value = "分页查询学院")
    @GetMapping(value = "")
    public PageDataDTO<AcademyDTO> findOnePage(
            @ApiParam(value = "页号，从0开始", required = true, defaultValue = "0") @RequestParam("page") @Min(0) int page,
            @ApiParam(value = "每页记录条数", required = true, defaultValue = "30") @RequestParam("size") @Min(1) @Max(100) int size,
            @ApiParam(value = "排序字段，例如：字段1,asc,字段2,desc") @RequestParam(value = "sort", required = false, defaultValue = "id,desc") String sort,
            @ApiParam(value = "学院名称") @RequestParam(value = "name", required = false) String name,
            @ApiParam(value = "学院编号") @RequestParam(value = "code", required = false) String code
    ) {
        return this.academyService.findOnePage(page, size, sort, name, code);
    }

    @ApiOperation(value = "新增学院")
    @PostMapping(value = "")
    public AcademyDTO create(@ApiParam(value = "学院信息", required = true) @RequestBody AcademyDTO academyDTO) {
        return this.academyService.create(academyDTO);
    }

    @ApiOperation(value = "删除学院")
    @DeleteMapping(value = "/{id}")
    public void delete(@ApiParam(value = "学院id", required = true) @PathVariable(name = "id") String id) {
        this.academyService.deleteById(id);
    }

    @ApiOperation(value = "更新学院信息")
    @PutMapping(value = "/{id}")
    public AcademyDTO update(
            @ApiParam(value = "学院id", required = true) @PathVariable(name = "id") String id,
            @ApiParam(value = "学院信息", required = true) @RequestBody AcademyDTO academyDTO
    ) {
        return this.academyService.update(id, academyDTO);
    }

    @ApiOperation(value = "根据Id查找学院信息")
    @GetMapping(value = "/{id}")
    public AcademyDTO findById(@ApiParam(value = "学院id", required = true) @PathVariable(name = "id") String id,
                               @ApiParam(value = "是否获取专业", required = true) @RequestParam(value = "isGetProfession") Boolean isGetProfession) {
        return this.academyService.findById(id, isGetProfession);
    }

    @ApiOperation(value = "查找所有学院")
    @GetMapping(value = "/all")
    public List<AcademyDTO> findAll() {
        return this.academyService.findAll();
    }

    @ApiOperation(value = "查找所有学院并排序")
    @GetMapping(value = "/all/sort")
    public List<AcademyDTO> findAllBySort(
            @ApiParam(value = "排序参数") @RequestParam(value = "param", required = false, defaultValue = "id") String param,
            @ApiParam(value = "排序模式: asc/desc") @RequestParam(value = "pattern", required = false, defaultValue = "asc") String pattern
    ) {
        return this.academyService.findAllBySort(param, pattern);
    }

    @ApiOperation(value = "检查学院编码是否存在")
    @GetMapping(value = "/only")
    public SimpleDataDTO<Boolean> checkoutCodeIsExist(
            @ApiParam(value = "学院编码code", required = true) @RequestParam(value = "code") String code
    ) {
        return this.academyService.checkoutCodeIsExist(code);
    }

    @ApiOperation(value = "统计学院数量")
    @GetMapping(value = "/count")
    public Long countAcademy() {
        return this.academyService.countAcademy();
    }
}